<?php

namespace App\Libraries;

class PhoneNumber {

    public function getType($number){
        $number_arr = str_split($number);       // 将号码拆成数字一维数组
        $format = 0;
        for ($i = 4; $i <= 8; $i++){
            $type = $this->after($number_arr, $i);
            if ($type){
                $format = $this->get_format($type);
                break;
            }
        }
        return ["format" => $format, 'type' => $type];
    }

    private function after($number, $len = 4) {
        if ($len == 4) {
            // 取后四位
            $number_4 = array_slice($number, -4);
            // 去重
            $uniqueDigits = array_unique($number_4);
            // 4位都一样
            if (count($uniqueDigits)){
                return "aaaa";
            }
            // 前3位一样
            if ($number_4[0] == $number_4[1] &&
                $number_4[1] == $number_4[2] &&
                $number_4[2] != $number_4[3]
            ){
                return "aaab";
            }
            // 前两位一样，后两位一样
            if ($number_4[0] == $number_4[1] &&
                $number_4[1] == $number_4[2] &&
                $number_4[3] != $number_4[4]
            ){
                return "aabb";
            }
            // 1 3一样 2 4一样
            if ($number_4[0] == $number_4[2] &&
                $number_4[1] == $number_4[3] &&
                $number_4[0] != $number_4[3]
            ){
                return "abab";
            }
            if ($number_4[0] - 1 == $number_4[1] &&
                $number_4[1] - 1 == $number_4[2] &&
                $number_4[2] - 1 == $number_4[3]
            ){
                return 'abcd';
            }
            if (($number_4[0] - 1 == $number_4[1]) &&
                ($number_4[1] - 1 == $number_4[2]) &&
                ($number_4[2] - 1 == $number_4[3]))
            {
                return "dcba";
            }
            if (($number_4[1] + 1 == $number_4[3])
                && ($number_4[2] + 1 == $number_4[3])
            ){
                return "abc";
            }
            if (($number_4[1] - 1 == $number_4[2])
                && ($number_4[2] - 1 == $number_4[3])
            ){
                return "cba";
            }
            if ($number_4[1] == $number_4[2]
                && $number_4[2] == $number_4[3]
            ){
                return "aaa";
            }

        } else if ($len == 5){
            $number_4 = array_slice($number, -5);
            // 去重
            $uniqueDigits = array_unique($number_4);
            // 4位都一样
            if (count($uniqueDigits)){
                return "aaaaa";
            }
            if ($number_4[0] == $number_4[1] &&
                $number_4[1] == $number_4[2] &&
                $number_4[2] == $number_4[3] &&
                $number_4[3] == $number_4[4])
            {
                return "aaaaa";
            }
            if ($number_4[0] == $number_4[1] &&
                $number_4[1] == $number_4[2] &&
                $number_4[2] == $number_4[3] &&
                ($number_4[3] != $number_4[4]))
            {
                return "aaaab";
            }
            if ($number_4[0] == $number_4[1] &&
                $number_4[1] == $number_4[2] &&
                $number_4[3] == $number_4[4] &&
                ($number_4[2] != $number_4[3]))
            {
                return "aaabb";
            }

            if ($number_4[0] == $number_4[1] &&
                $number_4[2] == $number_4[3] &&
                $number_4[3] == $number_4[4] &&
                ($number_4[1] != $number_4[2]))
            {
                return "aabbb";
            }
            if (($number_4[0] + 1 == $number_4[1]) &&
                ($number_4[1] + 1 == $number_4[2]) &&
                ($number_4[2] + 1 == $number_4[3]) &&
                ($number_4[3] + 1 == $number_4[4]))
            {
                return "abcde";
            }
            if (($number_4[0] - 1 == $number_4[1]) &&
                ($number_4[1] - 1 == $number_4[2]) &&
                ($number_4[2] - 1 == $number_4[3]) &&
                ($number_4[3] - 1 == $number_4[4]))
            {
                return "edcba";
            }

            return "";

        }else if ($len == 6) {
            $number_6 = array_slice($number, -6);
            // 去重
            $uniqueDigits = array_unique($number_6);
            // 4位都一样
            if (count($uniqueDigits)){
                return "aaaaaa";
            }
            $number_6 = $number;

            if ($number_6[2] == $number_6[3] &&
                $number_6[3] == $number_6[4] &&
                $number_6[5] == $number_6[6] &&
                $number_6[6] == $number_6[7] &&
                ($number_6[4] != $number_6[5]))
            {
                return "aaabbb";
            }
            if ($number_6[2] == $number_6[3] &&
                $number_6[3] == $number_6[4] &&
                $number_6[4] == $number_6[5] &&
                $number_6[5] == $number_6[6] &&
                ($number_6[6] != $number_6[7]))
            {
                return "aaaaab";
            }
            if ($number_6[2] == $number_6[3] &&
                $number_6[3] == $number_6[4] &&
                $number_6[4] == $number_6[5] &&
                $number_6[6] == $number_6[7] &&
                ($number_6[5] != $number_6[6]))
            {
                return "aaaabb";
            }
            if ($number_6[2] == $number_6[4] &&
                $number_6[4] == $number_6[6] &&
                $number_6[3] == $number_6[5] &&
                ($number_6[5] == $number_6[7] &&
                    $number_6[2] != $number_6[3]))
            {
                return "ababab";
            }
            if ($number_6[2] == $number_6[3] &&
                $number_6[4] == $number_6[5] &&
                $number_6[6] == $number_6[7] &&
                ($number_6[3] != $number_6[4] &&
                    $number_6[5] != $number_6[6]))
            {
                return "aabbcc";
            }
            //abcabc
            if ($number_6[2] == $number_6[5] &&
                $number_6[3] == $number_6[6] &&
                $number_6[4] == $number_6[7] &&
                (
                    $number_6[2] != $number_6[3] &&
                    $number_6[3] != $number_6[4] &&
                    $number_6[2] != $number_6[4]
                ) &&
                (
                    $number_6[2] + 1 == $number_6[3] &&
                    $number_6[3] + 1 == $number_6[4]
                )
            ){
                return "abcabc";
            }
            //abccba
            if ($number_6[2] == $number_6[7] &&
                $number_6[3] == $number_6[6] &&
                $number_6[4] == $number_6[5] &&
                ($number_6[2] != $number_6[3] &&
                    $number_6[3] != $number_6[4] &&
                    $number_6[2] != $number_6[4]) && ($number_6[2] + 1 == $number_6[3] && $number_6[3] + 1 == $number_6[4]))
            {
                return "abccba";
            }
            if (($number_6[2] + 1 == $number_6[3]) &&
                ($number_6[3] + 1 == $number_6[4]) &&
                ($number_6[4] + 1 == $number_6[5]) &&
                ($number_6[5] + 1 == $number_6[6]) &&
                ($number_6[6] + 1 == $number_6[7]))
            {
                return "abcdef";
            }
            if (($number_6[2] - 1 == $number_6[3]) &&
                ($number_6[3] - 1 == $number_6[4]) &&
                ($number_6[4] - 1 == $number_6[5]) &&
                ($number_6[5] - 1 == $number_6[6]) &&
                ($number_6[6] - 1 == $number_6[7]))
            {
                return "fedcba";
            }

            return "";

        }else if ($len == 7) {
            $number_6 = array_slice($number, -7);
            // 去重
            $uniqueDigits = array_unique($number_6);
            // 4位都一样
            if (count($uniqueDigits)){
                return "aaaaaaa";
            }

            if ($number[1] == $number[2] &&
                $number[2] == $number[3] &&
                $number[3] == $number[4] &&
                $number[5] == $number[6] &&
                $number[6] == $number[7] &&
                ($number[4] != $number[5]))
            {
                return "aaaabbb";
            }
            if ($number[1] == $number[2] &&
                $number[2] == $number[3] &&
                $number[3] == $number[4] &&
                $number[4] == $number[5] &&
                $number[5] == $number[6] &&
                $number[6] != $number[7])
            {
                return "aaaaaab";
            }
            return "";
        }else if ($len == 8) {
            $number_6 = array_slice($number, -8);
            // 去重
            $uniqueDigits = array_unique($number_6);
            // 4位都一样
            if (count($uniqueDigits)){
                return "aaaaaaaa";
            }
            //abcdabcd
            if ($number[0] == $number[4] &&
                $number[1] == $number[5] &&
                $number[2] == $number[6] &&
                $number[3] == $number[7] )
            {

                return "abcdabcd";
            }
            //abcddcba
            if (($number[0] == $number[7]) &&
                ($number[1] == $number[6]) &&
                ($number[2] == $number[5]) &&
                ($number[3] == $number[4]) && ($number[0] != $number[1] && $number[1] != $number[2] && $number[2] != $number[3]) && ($number[0] - 1 == $number[1] && $number[1] - 1 == $number[2] && $number[2] - 1 == $number[3]))
            {
                return "abcddcba";
            }
            return "";

        }
    }

    private function get_format($productTypes) {
        switch ($productTypes) {
            case 'aaa':
            case 'aaaa':
            case 'abcd':
            case 'abcdabcd':
            case 'aaaab':
            case 'aaaaa':
            case 'aabbb':
            case 'abcde':
            case 'aaaaaa':
            case 'aaabbb':
            case 'abcdef':
            case 'aaaaab':
            case 'aaaaaaa':
            case 'aaaabbb':
            case 'aaaaaaaa':
                $result = 1;
                break;
            case 'aaab':
            case 'aabb':
            case 'abab':
            case 'abc':
            case 'edcba':
            case 'aabbcc':
            case 'aaaabb':
            case 'aaabb':
            case 'abcabc':
            case 'ababab':
            case 'fedcba':
                $result = 2;
                break;
            default:
                $result = 0;
                break;
        }
        return $result;
    }


}